﻿using Marbles.CMS.Core;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using Marbles.CMS.ContentTypes;


namespace Marbles.CMS.ContentTypes
{
    public abstract class PageRendererBase : Marbles.Core.Web.Templates.Page, IPageRenderer
    {


        public PageDefinition PageDefinition
        {
            get { return this._PageDefinition; }
            set { this._PageDefinition = value; }
        }

        private PageDefinition _PageDefinition = null;

        protected override void OnPreInit(EventArgs e)
        {
            HtmlFormatter fmnt = new HtmlFormatter();                        
            base.OnPreInit(e);
            this.MasterPageFile = this._PageDefinition.TemplateSourcePath;
            this.Theme = this._PageDefinition.ThemeName;
            this.Title = fmnt.Format(this._PageDefinition.Title, HttpContext.Current);

            ITemplate template = new CompiledTemplateBuilder(new BuildTemplateMethod(delegate(Control c) { c.Controls.Add(CreateMetaTag("keywords", string.Empty, this._PageDefinition.Keywords)); c.Controls.Add(CreateMetaTag("description", string.Empty, this._PageDefinition.Description)); }));
            AddContentTemplate("head", template);

            RenderPageContent(this._PageDefinition);
        }

        public abstract void RenderPageContent(PageDefinition Definition);

        public void AddContentToPlaceHolder(string key, ITemplate Template)
        {
            AddContentTemplate(key, Template);
        }

        protected virtual ITemplate CreateContentTemplate(string Key, string Value)
        {
            ITemplate contenttemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(delegate(Control c) { c.Controls.Add(new LiteralControl(FormatContent(Value))); }));
            return contenttemplate;
        }

        protected virtual string FormatHtmlContent(string html)
        {
            return html;
        }

        private string FormatContent(string html)
        {
            HtmlFormatter fmnt = new HtmlFormatter();
            string html1 = fmnt.Format(html, HttpContext.Current);
            return FormatHtmlContent(html1);
        }

        public string RemoveKeyFromUrl(string key)
        {
            int i = 0;
            Uri url = HttpContext.Current.Request.Url;
            StringBuilder sb = new StringBuilder();
            sb.Append(url.Scheme);
            sb.Append(":");
            if (url.Port != 80) { sb.Append(url.Port); }
            sb.Append("//");
            sb.Append(url.Host);
            sb.Append(url.AbsolutePath);

            if (Request.QueryString.Count > 0)
            {
                foreach (string s in Request.QueryString.Keys)
                {
                    if (s.ToLower() != key.ToLower())
                    {
                        if (i == 0) { sb.Append("?"); } else { sb.Append("&"); }
                        sb.AppendFormat("{0}={1}", Request.QueryString.Keys[i], Request.QueryString[i]);
                        i += 1;
                    }
                }
            }

            return sb.ToString();
        }

        public string AppendKeyToUrl(string key, object value)
        {
            bool created = false;
            Uri url = HttpContext.Current.Request.Url;
            StringBuilder sb = new StringBuilder();
            sb.Append(url.Scheme);
            sb.Append(":");
            if (url.Port != 80) { sb.Append(url.Port); }
            sb.Append("//");
            sb.Append(url.Host);
            sb.Append(url.AbsolutePath);

            if (Request.QueryString.Count > 0)
            {
                for (int i = 0; i < Request.QueryString.Count; i++)
                {
                    if (i == 0) { sb.Append("?"); } else { sb.Append("&"); }

                    if (Request.QueryString.Keys[i].ToLower() == key.ToLower())
                    {
                        sb.AppendFormat("{0}={1}", key.ToLower(), value);
                        created = true;
                    }
                    else
                    {
                        sb.AppendFormat("{0}={1}", Request.QueryString.Keys[i], Request.QueryString[i]);
                    }
                }

                if (!created) { sb.AppendFormat("&{0}={1}", key.ToLower(), value); }
            }
            else
            {
                if (!created) { sb.AppendFormat("?{0}={1}", key.ToLower(), value); }
            }



            return sb.ToString();
        }

    }
}
